﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using log4net;
using Mate.Caching;
using Mate.Core;
using Mate.Core.Base;
using Mate.Db.Model.System;
using Mate.Dto.System;
using Mate.Framework.BaseController.Admin;
using Mate.System.Service;
using Microsoft.AspNetCore.Mvc;

namespace Mate.Admin.Controllers.System
{
    public class MenuController : BaseController
    {
        private readonly IMenuService menuService;
        private readonly ILog log;
        private readonly ICacheManager _cache;
        public MenuController(IMenuService menuService, ICacheManager cache, ILog log)
        {
            this.log = log;
            this.menuService = menuService;
            this._cache = cache; 
        }

        #region 列表页面
        public ActionResult Index()
        {
            var menuList = menuService.GetList(x => x.id > 0, x => x.order_by, SqlSugar.OrderByType.Desc);
            var result = menuList.Where(x => x.parent_id == 0)
                .Select(x => new MenuModel()
                {
                    id = x.id,
                    menu_name = x.menu_name,
                    parent_id = x.parent_id,
                    menu_type = x.menu_type,
                    action_name = x.action_name,
                    controller_name = x.controller_name,
                    order_by = x.order_by,
                    icons = x.icons,
                    full_name = x.full_name,
                    url_parameter = x.url_parameter,
                    children = menuList.Where(y => y.parent_id == x.id)
                    .Select(y => new MenuModel()
                    {
                        id = y.id,
                        menu_name = y.menu_name,
                        parent_id = y.parent_id,
                        menu_type = y.menu_type,
                        action_name = y.action_name,
                        controller_name = y.controller_name,
                        order_by = y.order_by,
                        icons = y.icons,
                        full_name = y.full_name,
                        url_parameter = y.url_parameter,
                        children = menuList.Where(z => z.parent_id == y.id)
                        .Select(z => new MenuModel()
                        {
                            id = z.id,
                            menu_name = z.menu_name,
                            parent_id = z.parent_id,
                            menu_type = z.menu_type,
                            action_name = z.action_name,
                            controller_name = z.controller_name,
                            order_by = z.order_by,
                            icons = z.icons,
                            full_name = z.full_name,
                            url_parameter = z.url_parameter,
                        }).ToList()
                    }).ToList()
                }).ToList();
            return View(result);
        }
        #endregion



        #region 添加
        public ActionResult Create()
        {

            ViewBag.MenuList = menuService.GetList(x => x.id > 0, x => x.menu_type == 1);
            var model = new menu() { menu_type = 1, order_by = 99};
            return View(model);
        }

        [HttpPost]
       // [ValidateInput(false)]
        public JsonResult Create(menu model)
        {
            var result = new BaseResult();
            model.is_delete = false;
            var objInt = menuService.Insert(model);
            if (objInt > 0)
            {
                _cache.RemoveByPattern(string.Format(MateCacheKey.CacheRoleIdMenuCache, ""));
                result.ResultCode = ResultCode.Success;
                result.Message = "添加成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "添加失败";
            }
            return Json(result);
        }
        #endregion

        #region 修改
        public ActionResult Edit(int id)
        {
            var model = menuService.Get(x => x.id == id);
            if (model == null)
            {
                return Content("参数错误");
            }
            ViewBag.MenuList = menuService.GetList(x=>x.id>0,x=>x.menu_type == 1);
            return View(model);
        }

        [HttpPost]
        //[ValidateInput(false)]
        public JsonResult Edit(menu model)
        {
            var result = new BaseResult();

            var b = menuService.Update(model);
            if (b > 0)
            {
                _cache.RemoveByPattern(string.Format(MateCacheKey.CacheRoleIdMenuCache, ""));
                result.ResultCode = ResultCode.Success;
                result.Message = "修改成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "修改失败";
            }
            return Json(result);

        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var result = new BaseResult();

            var b = menuService.Delete(x => x.id == id || x.parent_id == id);
            if (b > 0)
            {
                _cache.RemoveByPattern(string.Format(MateCacheKey.CacheRoleIdMenuCache, ""));
                result.ResultCode = ResultCode.Success;
                result.Message = "删除成功";
            }
            else
            {
                result.ResultCode = ResultCode.Fail;
                result.Message = "删除失败";
            }
            return Json(result);
        }
        #endregion

    }
}